//
//  ClassifyDataBase.m
//  Happy Kitchen
//
//  Created by 冫柒Yun on 15/3/12.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "ClassifyDataBase.h"

@implementation ClassifyDataBase

+ (instancetype)sharkClassifyDataBase
{
    static ClassifyDataBase *classifyDataBase = nil;
    if (classifyDataBase == nil) {
        classifyDataBase = [[ClassifyDataBase alloc] init];
    }
    return classifyDataBase;
}
// 创建一个数据库对象
static sqlite3 *db;

// 打开数据库
- (void)openDB
{
    // 获取沙盒路径
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    // 取出document路径
    NSString *filePath = [array lastObject];
    // 拼接一个sqlite文件路径
    NSString *sqlitePath = [filePath stringByAppendingPathComponent:@"Classify.sqlite"];
    NSLog(@"路径 = %@", sqlitePath);
    
    // 判断数据库是否打开
    if (db != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
    // 打开数据库
    int result = sqlite3_open(sqlitePath.UTF8String, &db);
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else{
        NSLog(@"数据库打开失败");
    }
}
// 创建数据库表
- (void)createTable
{
    // 生成创建表的sql语句
    NSString *createSql = @"CREATE TABLE IF NOT EXISTS classifyTable(number INTEGER PRIMARY KEY AUTOINCREMENT, RecipeId TEXT, Title TEXT)";
    // 执行sql语句
    int result = sqlite3_exec(db, createSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建数据库表成功");
    }else{
        NSLog(@"创建数据库表失败");
    }
}
// 往数据库表中插入数据
- (void)insertClassifyDetail:(DetailModel *)classifyDetail
{
    // 创建插入活动的sql语句
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO classifyTable(RecipeId, Title) VALUES('%@', '%@')", classifyDetail.RecipeId, classifyDetail.Title];
    // 执行sql语句
    int result = sqlite3_exec(db, insertSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}
// 查询所有菜谱信息
- (NSMutableArray *)selectClassifyDetail
{
    NSString *selectSql = @"SELECT *FROM classifyTable";
    // 创建数据库跟随指针, 用来循环遍历数据库中的每行数据
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, selectSql.UTF8String, -1, &stmt, nil);
    
    // 创建存放活动对象的大数组
    NSMutableArray *array = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        
        // 开始循环遍历查询数据库每一行
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 当跟随指针指向的行的数据符合你得查询条件, 就在while里返回
            // 参数2.指的是在数据库表中的第几列
            // 逐列取出每一列的数据
            const unsigned char *recipeId = sqlite3_column_text(stmt, 1);
            const unsigned char *title = sqlite3_column_text(stmt, 2);
            
            
            NSString *recipeIdStr = [NSString stringWithUTF8String:(const char *)recipeId];
            NSString *titleStr = [NSString stringWithUTF8String:(const char *)title];
            
            
            DetailModel *classifyDetail = [[DetailModel alloc] init];
            classifyDetail.Title = titleStr;
            classifyDetail.RecipeId = recipeIdStr;
            
            // 将活动对象存入大数组中
            [array addObject:classifyDetail];
        }
        
        
    }else{
        NSLog(@"查询失败");
    }
    
    return array;
}
// 删除一个数据
- (void)deleteClassify:(NSString *)title
{
    // 创建删除菜谱Sql语句
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM classifyTable WHERE Title = '%@'", title];
    // 执行sql语句
    int result = sqlite3_exec(db, deleteSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}
// 删除数据库表
- (void)DropTable
{
    NSString *dropSql = @"DROP TABLE classifyTable";
    int result = sqlite3_exec(db, dropSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"清除表成功");
    }else{
        NSLog(@"清除表失败");
    }
}

@end
